<?php

/**
 * article actions.
 *
 * @package    skiaplanche.ca
 * @subpackage article
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class articleActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $this->sap_articles = Doctrine::getTable('sap_article')
          ->createQuery('a')
          ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new sap_articleForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new sap_articleForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($sap_article = Doctrine::getTable('sap_article')->find(array($request->getParameter('id'))), sprintf('Object sap_article does not exist (%s).', $request->getParameter('id')));
    $response = $this->getResponse();
    $response->setTitle( 'Edit -> ' . $sap_article->title );
    $this->form = new sap_articleForm($sap_article);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($sap_article = Doctrine::getTable('sap_article')->find(array($request->getParameter('id'))), sprintf('Object sap_article does not exist (%s).', $request->getParameter('id')));
    $this->form = new sap_articleForm($sap_article);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($sap_article = Doctrine::getTable('sap_article')->find(array($request->getParameter('id'))), sprintf('Object sap_article does not exist (%s).', $request->getParameter('id')));
    $sap_article->delete();

    $this->redirect('article/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sap_article = $form->save();

      $this->redirect('article/edit?id='.$sap_article->getId());
    }
  }
}
